package com.gaofei.pdf.controller;

import com.bw.itext.pdf.service.PdfService;
import com.gaofei.pdf.domain.House;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
public class PdfController {

    @Autowired
    private PdfService pdfService;

    /**
     * 预览pdf文件
     * @param response
     * @throws IOException
     */

    @RequestMapping("preview")
    public void preview (HttpServletResponse response) throws IOException {
        pdfService.preview("house.sign.pdf",response);
    }

    /**
     * 下载pdf文件
     * @param response
     * @throws IOException
     */

    @RequestMapping("down")
    public void download (HttpServletResponse response) throws IOException {
        pdfService.download("house.sign.pdf",response);
    }


    /**
     * 生产pdf并且签章
     * @return
     * @throws IOException
     */
    @RequestMapping("pdf")
    public String pdf () throws IOException {
        List<House> list = new ArrayList<>();//模拟从数据库查询出来的house列表
        House house = new House();
        house.setId(1);
        house.setAddress("北京市");
        house.setCommunity("环境优美");
        list.add(house);
        House house1 = new House();
        house1.setId(2);
        house1.setAddress("天津市");
        house.setCommunity("环境优美");
        list.add(house);

        //=============生成pdf=============
        pdfService.export(list,"house.pdf","房屋集合",House.class);
        //=========给pdf签章============
        pdfService.sign("house.pdf","house.sign.pdf");
        return "pdf success!";
    }
}
